/*!
\copyright  Copyright (c) 2024 Qualcomm Technologies International, Ltd.
            All Rights Reserved.
            Qualcomm Technologies International, Ltd. Confidential and Proprietary.
\file       
\brief      App handling of device delete requests
*/

#include "app_device_delete.h"

#include <bdaddr.h>
#include <device_channel_event_spec.h>
#include <handset_service.h>
#include <logging.h>
#include <sync_event.h>

#if defined (INCLUDE_STEREO) || defined (ENABLE_TWM_STEREO)
#include <headset_sm.h>
#endif /* (INCLUDE_STEREO) || (ENABLE_TWM_STEREO) */

static void appDeviceDelete_HandleEvent(event_bus_channel_t channel, event_bus_event_t event, void * data, uint16 data_size, const event_bus_subscriber_t* subscriber);
static void appDeviceDelete_MessageHandler(Task task, MessageId id, Message msg);

static event_bus_subscriber_t add_event_subscriber = {appDeviceDelete_HandleEvent, NULL};
static const TaskData add_task_data = {appDeviceDelete_MessageHandler};

static const event_bus_event_t add_sync_device_events[] = {device_event_delete_request};

static void appDeviceDelete_MessageHandler(Task task, MessageId id, Message msg)
{
    UNUSED(task);
    UNUSED(id);
    UNUSED(msg);
}

static bool appDeviceDelete_IsPrimary(void)
{
#ifdef INCLUDE_MIRRORING
#ifdef ENABLE_TWM_STEREO
    return IsTwmActive() ? BtDevice_IsMyAddressPrimary() : TRUE;
#else
    return BtDevice_IsMyAddressPrimary();
#endif /* ENABLE_TWM_STEREO */
#else
    return TRUE;
#endif /* INCLUDE_MIRRORING */
}

static void appDeviceDelete_HandleDeviceEvent(event_bus_event_t event, void * data, uint16 data_size)
{
    device_event_data_t* event_data = (device_event_data_t*)data;
    UNUSED(data_size);
    
    DEBUG_LOG_VERBOSE("appDeviceDelete_HandleDeviceEvent: enum:device_event_t:%d addr [%04x,%02x,%06lx]", event, event_data->tpaddr.taddr.addr.nap, event_data->tpaddr.taddr.addr.uap, event_data->tpaddr.taddr.addr.lap);
    
    switch(event)
    {
        case device_event_delete_request:
        {
            device_t device = BtDevice_GetDeviceForTpbdaddr(&event_data->tpaddr);
            deviceType type = BtDevice_GetDeviceType(device);
            
            if(appDeviceDelete_IsPrimary() && (type == DEVICE_TYPE_HANDSET))
            {
                DEBUG_LOG_ALWAYS("appDeviceDelete_HandleDeviceEvent on primary - try handset disconnect");
                HandsetService_DisconnectTpAddrRequest((Task)&add_task_data, &event_data->tpaddr, 0);
            }
            else
            {
                /* This indicates a call to BtDevice_SafeDelete for an unhandled device type.
                   Ensure all clients with a dependency on this device type use DeviceProperties_RegisterClient
                   to register the dependency. Optionally, handling to actively manage removing dependencies
                   can be added here, otherwise the device will be deleted when DeviceProperties_GetClientCount
                   reaches 0. */
                DEBUG_LOG_WARN("appDeviceDelete_HandleDeviceEvent - unhandled device type enum:deviceType:%d", type);
            }
        }
        break;
        
        case device_event_deleted_remote:
            DEBUG_LOG_ALWAYS("appDeviceDelete_HandleDeviceEvent device_event_deleted_remote");
        break;
        
        default:
        break;
    }
}

static void appDeviceDelete_HandleEvent(event_bus_channel_t channel, event_bus_event_t event, void * data, uint16 data_size, const event_bus_subscriber_t* subscriber)
{
    UNUSED(subscriber);
    
    switch(channel)
    {
        case EVENT_BUS_CHANNEL_DEVICE:
            appDeviceDelete_HandleDeviceEvent(event, data, data_size);
        break;
        
        default:
        break;
    }
}

bool AppDeviceDelete_Register(Task task)
{
    UNUSED(task);

    SyncEvent_SubscribeSyncEventsArray(EVENT_BUS_CHANNEL_DEVICE, add_sync_device_events, add_event_subscriber);

    return TRUE;
}
